package com.example.reggie.interceptor;

import com.alibaba.fastjson2.JSON;
import com.example.reggie.common.BaseContext;
import com.example.reggie.common.R;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

public class ReggieInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Long empId = (Long) request.getSession().getAttribute("employee");
        if(empId!=null){
            BaseContext.setCurrentId(empId);
            return true;
        }else {
            response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
//            response.getOutputStream().print(JSON.toJSONString(R.error("NOTLOGIN")));
        }
        return false;
    }
}
